﻿using System;
using System.Diagnostics;
using Loki.Game;
using Loki.Utilities.Plugins;

namespace FlaskHelper
{
    public class Main : IPlugin
    {

        #region Implementation of IEquatable<IPlugin>

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public bool Equals(IPlugin other)
        {
            return Name.Equals(other.Name);
        }

        #endregion

        #region Implementation of IPlugin

        public string Author { get { return "toNyx"; } }
        public Version Version { get { return new Version(1, 0, 0, 1); } }
        public string Name { get { return "FlaskHelper"; } }
        public string Description { get { return "Optimized flask usage"; } }

        // Custom Vars
        private static readonly Stopwatch CheckStuff = Stopwatch.StartNew();

        /// <summary>
        /// Executes the initialize action. This is called at initial bot startup. (When the bot itself is started, not when Start() is called) 
        /// </summary>
        public void OnInitialize()
        {
        }

        /// <summary>
        /// Executes the pulse action. This is called every "tick" of the bot. 
        /// </summary>
        public void OnPulse()
        {
            if (LokiPoe.ObjectManager.Me.IsInTown) return; // If We're in town, we don't need to check

            if (CheckStuff.ElapsedMilliseconds <= 1000) return; // Reduce CPU Usage (Modify to make flask usage faster (but not always better)

            // Reset 1 second CD
            CheckStuff.Restart();

            Helpers.CheckFlaskUsage(Flasks.FlaskTypes.Life, FlaskHelperSettings.Instance.LifeFlaskThreshold);
            Helpers.CheckFlaskUsage(Flasks.FlaskTypes.Mana, FlaskHelperSettings.Instance.ManaFlaskThreshold);

            if (FlaskHelperSettings.Instance.UseGraniteFlask)
                Helpers.CheckFlaskUsage(Flasks.FlaskTypes.Granite, FlaskHelperSettings.Instance.GraniteFlaskThreshold);

            if (FlaskHelperSettings.Instance.UseJadeFlask)
                Helpers.CheckFlaskUsage(Flasks.FlaskTypes.Jade, FlaskHelperSettings.Instance.JadeFlaskThreshold);

            if (FlaskHelperSettings.Instance.UseRubyFlask)
                Helpers.CheckFlaskUsage(Flasks.FlaskTypes.Ruby, FlaskHelperSettings.Instance.RubyFlaskThreshold);

            if (FlaskHelperSettings.Instance.UseDiamondFlask)
                //Helpers.CheckFlaskUsage(Flasks.FlaskTypes.Diamond, 0); // TODO: Implementation

            if (FlaskHelperSettings.Instance.UseBleedingFlask)
                Helpers.CheckFlaskUsage(Flasks.FlaskTypes.Bleeding, 0); // TODO: Check if this isn't buggy

            if (FlaskHelperSettings.Instance.UseBleedingFlask)
                Helpers.CheckFlaskUsage(Flasks.FlaskTypes.Shock, 0);

            if (FlaskHelperSettings.Instance.UseBleedingFlask)
                Helpers.CheckFlaskUsage(Flasks.FlaskTypes.Burning, 0);

            if (FlaskHelperSettings.Instance.UseBleedingFlask)
                Helpers.CheckFlaskUsage(Flasks.FlaskTypes.Frozen, 0);

            if (FlaskHelperSettings.Instance.UseBleedingFlask)
                Helpers.CheckFlaskUsage(Flasks.FlaskTypes.Divination, 0);
        }

        /// <summary>
        /// Executes the start action. This is called when the bot starts. 
        /// </summary>
        public void OnStart()
        {

        }

        /// <summary>
        /// Executes the stop action. This is called when the bot is stopped. 
        /// </summary>
        public void OnStop()
        {

        }

        /// <summary>
        /// Executes the shutdown action. This is called when the bot is shutting down. (Not when Stop() is called) 
        /// </summary>
        public void OnShutdown()
        {

        }

        /// <summary>
        /// Executes the enabled action. This is called when the user has enabled this specific plugin via the GUI. 
        /// </summary>
        public void OnEnabled()
        {

        }

        /// <summary>
        /// Executes the disabled action. This is called whent he user has disabled this specific plugin via the GUI. 
        /// </summary>
        public void OnDisabled()
        {

        }

        /// <summary>
        /// Executes the config action. This is called when the user clicks on the config button.
        /// </summary>
        public void OnConfig()
        {
            var mySettingsForm = new SettingsForm();
            mySettingsForm.Show();
        }

        #endregion

    }
}
